package com.example.statistics.ui.product

import android.os.Bundle
import android.util.Log
import android.view.View
import com.example.statistics.R
import com.example.statistics.bean.FilesBean
import com.example.statistics.bean.ProductBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.httpGet
import com.example.statistics.ex.httpPost
import com.example.statistics.ex.isNumber
import com.example.statistics.ex.singleDialog
import com.example.statistics.net.UrlConfig
import com.example.statistics.net.callback.UploadDialogHttpCallback
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.jhj.httplibrary.HttpCall
import kotlinx.android.synthetic.main.activity_product_edit.*
import kotlinx.android.synthetic.main.activity_product_edit.layout_image
import kotlinx.android.synthetic.main.activity_product_edit.tv_product_no
import kotlinx.android.synthetic.main.activity_product_edit.tv_remark
import kotlinx.android.synthetic.main.activity_product_info_edit.*
import org.jetbrains.anko.toast
import java.io.File

class ProductEditActivity : BaseActivity() {

    var productId = 0
    var imageList = mutableListOf<FilesBean>()

    override val title: String
        get() = "商品详情"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_product_edit)
        productId = intent.getIntExtra(Constants.ID, 0)
        if (productId != 0) {
            layout_image.visibility = View.VISIBLE
            httpGet<ProductBean>(UrlConfig.PRODUCT_INFO_DATA, config = {
                listParams = listOf("id" to productId)
            }) {
                it?.let {
                    tv_type.contentText = it.productType
                    tv_nation.contentText = it.nation
                    tv_company.contentText = it.company
                    tv_factory.contentText = it.factoryNo
                    tv_product.contentText = it.productName
                    tv_product_no.contentText = it.productNo
                    tv_specification.contentText = if (it.specification.isNumber()) it.specification + "KG" else it.specification
                    tv_remark.contentText = it.remark
                    imageList = it.imageList.orEmpty().toMutableList()
                    layout_image.setImageList(imageList.map { UrlConfig.BASE_URL + it.path })
                }
            }
        } else {
            layout_image.visibility = View.GONE
        }

        tv_type.onContentClicked {
            singleDialog("猪副", "牛副", "鸡副") {
                tv_type.contentText = it
            }
        }
        tv_specification.onContentClicked {
            singleDialog("5KG", "8KG", "10KG", "12KG", "13.6KG", "13.61KG", "15KG", "18KG", "20KG", "25KG", "非定装") {
                tv_specification.contentText = it
            }
        }

        layout_image.imageDelete {
            val name = it.substring(it.lastIndexOf("/"), it.length)
            val bean = imageList.find { image -> image.name == name }
            if (bean == null) {
                toast("删除失败")
                return@imageDelete
            }
            httpGet<Any>(UrlConfig.PRODUCT_FILE_DELETE, config = {
                listParams = listOf("id" to bean.id)
            }) {
                toast("删除成功")
            }
        }

        layout_image.imageSelector {
            HttpCall.post(UrlConfig.PRODUCT_FILE_SAVE)
                .addFiles("files[]", it?.map { media -> File(media.compressPath) }.orEmpty())
                .addParam("type", "product")
                .addParam("identification", productId)//标识
                .addParam("folder", tv_factory.contentText + "/" + tv_product.contentText)//文件夾
                .enqueue(object : UploadDialogHttpCallback<List<FilesBean>>(this) {
                    override fun onSuccess(data: List<FilesBean>?, resultType: ResultType) {
                        imageList.addAll(data.orEmpty())
                        toast("保存成功")
                    }
                })
        }

        topBarRightText("保存") {
            val url = if (productId == 0) {
                UrlConfig.PRODUCT_INFO_ADD
            } else {
                UrlConfig.PRODUCT_INFO_UPDATE
            }

            httpPost<ProductBean>(url, config = {
                isShowDialog = true
                listParams = listOf(
                    "id" to productId,
                    "productType" to tv_type.contentText,
                    "nation" to tv_nation.contentText,
                    "company" to tv_company.contentText,
                    "factoryNo" to tv_factory.contentText,
                    "productName" to tv_product.contentText,
                    "productNo" to tv_product_no.contentText,
                    "specification" to tv_specification.contentText?.replace("KG", ""),
                    "remark" to tv_remark.contentText
                )
            }) {
                if (productId == 0) {
                    layout_image.visibility = View.VISIBLE
                } else {
                    activityFinish()
                }
                productId = it?.id ?: 0
            }
        }
    }
}